using System;
using System.ComponentModel.Composition.Hosting;
using System.Windows;
using Microsoft.Practices.Prism.MefExtensions;
using Mysoft.ServerMonitoring.OMKit.Views;
using ProjTemp.Desktop.Common;
using ProjTemp.Common.Logging;
using ProjTemp.Desktop.Common.ResourceManagement;
using ProjTemp.Common;
using Microsoft.Practices.Prism.Logging;
using System.Reflection;
using System.ComponentModel.Composition;

namespace Mysoft.ServerMonitoring.OMKit
{
    public partial class SysBootstrapper : MefBootstrapper
    {
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SysBootstrapper).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ILogger).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(IResourceManage).Assembly));
            DirectoryCatalog catalog = new DirectoryCatalog(DesktopConst.DiscoverModulesDirectory);
            this.AggregateCatalog.Catalogs.Add(catalog);
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

#if SILVERLIGHT
            Application.Current.RootVisual = (Shell)this.Shell;            
#else
            Application.Current.MainWindow = (Shell)this.Shell;
            Application.Current.MainWindow.Show();
#endif
        }

        protected override void RegisterBootstrapperProvidedTypes()
        {
            base.RegisterBootstrapperProvidedTypes();
            //Resource Manager
            var resourceManager = this.InitializeResourceManager();
        }

        protected override Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();

            //factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));

            return factory;
        }

        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();
        }

        protected virtual IResourceManage InitializeResourceManager()
        {
            try
            {
                var resourceManager = this.Container.GetExportedValue<IResourceManage>();
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Error.en-US.xaml") }, DesktopConst.LanguageResourceSubject, "en-US");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Message.en-US.xaml") }, DesktopConst.LanguageResourceSubject, "en-US");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Resource.en-US.xaml") }, DesktopConst.LanguageResourceSubject, "en-US");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Validation.en-US.xaml") }, DesktopConst.LanguageResourceSubject, "en-US");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Error.zh-CN.xaml") }, DesktopConst.LanguageResourceSubject, "zh-CN");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Message.zh-CN.xaml") }, DesktopConst.LanguageResourceSubject, "zh-CN");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Resource.zh-CN.xaml") }, DesktopConst.LanguageResourceSubject, "zh-CN");
                resourceManager.AddResourceDictionary(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Mysoft.ServerMonitoring.OMKit.Resource;component/Lang/Validation.zh-CN.xaml") }, DesktopConst.LanguageResourceSubject, "zh-CN");
                resourceManager.SetDefaultAspect(DesktopConst.LanguageResourceSubject, "zh-CN");
                resourceManager.ApplyToResourceDictionary(Application.Current.Resources);

                return resourceManager;
            }
            catch (Exception ex)
            {
                this.Logger.Log(string.Format(GlobalConst.ExceptionOccurs, MethodBase.GetCurrentMethod().Name, ex), Category.Exception, Priority.High);
            }

            return null;
        }
    }
}
